package com.ATMgui;

import com.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GetMoney extends JPanel implements ActionListener {
    private Icon sureBtn = Util.getIcon("/image/buttonSure.jpg");
    private JButton sure;
    private ATM frame;
    private JTextField textField1;

    public GetMoney(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        textField1 = new JTextField();
        textField1.setBounds(250, 150, 250, 30);
        this.add(textField1);

        this.setOpaque(false);
        sure = new JButton(sureBtn);
        sure.setBounds(200, 320, 100, 20);
        this.add(sure);
        sure.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sure) {
            ArrayList<Users> list = (ArrayList) FileUtil.readObject("d:/1.dat");
            if (Double.valueOf(textField1.getText()) <= list.get(frame.getNum()).getBlance()) {
                list.get(frame.getNum()).setBlance(list.get(frame.getNum()).getBlance() - Double.valueOf(textField1.getText()));
                JOptionPane.showMessageDialog(this, "取钱成功");
                FileUtil.writeObject("d:/1.dat", list);
            } else {
                JOptionPane.showMessageDialog(this, "余额不足");
            }
            frame.switchPanel("菜单");
        }
    }
}
